package com.meihua.baoleyi.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Button;

import com.meihua.baoleyi.common.Constants;
import com.meihua.baoleyi.utils.ToastUtil;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * 作者：lc on 2017/11/1.
 * 邮箱：lc0607live@163.com
 */

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = WXPayEntryActivity.class.getSimpleName();
    private static final String ACTION_NAME1 = "OrderFragment";
    private static final String ACTION_NAME = "PayDialog";

    private IWXAPI api;
    private String result;
    private String paymentStatus = "";
    private Button btReturn;
    private Intent mIntent;
    private Intent mIntent1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        api.handleIntent(getIntent(), this);
        mIntent = new Intent(ACTION_NAME);
        mIntent1 = new Intent(ACTION_NAME1);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    protected void onResume() {
        super.onResume();
//        ToastUtil.show(WXPayEntryActivity.this, "WXPayEntryActivity");
        finish();
    }

    @Override
    public void onResp(BaseResp resp) {
//        ToastUtil.show(WXPayEntryActivity.this, resp.getType());
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (resp.errCode == 0) {
                ToastUtil.show(WXPayEntryActivity.this, "支付成功");
                this.sendBroadcast(mIntent);
                this.sendBroadcast(mIntent1);
            } else if (resp.errCode == -1) {
                ToastUtil.show(WXPayEntryActivity.this, "支付失败");
            }
//            String num = Constants.ORDER_NUM;
//            String type = Constants.TYPE;
//            String url=UrlContants.WXING_JIAQIAN_MYBALAN;
//            if (type.equals("orderpay")){
//                url = UrlContants.WXING_JIAQIAN_ORDER;
//            }else if (type.equals("vippay")){
//                url = UrlContants.WXING_JIAQIAN_VIP;
//            }else if (type.equals("findpay")){
//                url = UrlContants.WXING_JIAQIAN_ORDER;
//            }else if (type.equals("balanpay")){
//                url = UrlContants.WXING_JIAQIAN_MYBALAN;
//            }else if (type.equals("findshop")){
//                url = UrlContants.WXSHOPP_JIAQIAN_PAY;
//            }
//            OkGo.post(url)
//                    .params("user_id", SharedPreUtils.getString("user_id", "", this))
//                    .params("order_sn",num)
//                    .params("order_num", num)
//                    .execute(new StringCallback() {
//                        @Override
//                        public void onSuccess(String s, Call call, Response response) {
//                            JSONObject jsonob = JSONObject.parseObject(s);
//                            String msg = jsonob.getString("msg");
//                            if (jsonob.getInteger("code") == 1) {
//                                Toast.makeText(WXEntryActivity.this, msg, Toast.LENGTH_SHORT).show();
//                                if (!"findpay".equals(type)&&!"findshop".equals(type)){
//                                    finish();
//                                }else {
//                                    Intent in = new Intent();
//                                    in.setClass(WXEntryActivity.this, MyOrderActivity.class);
//                                    startActivity(in);
//                                    finish();
//                                }
//                            } else {
//                                Toast.makeText(WXEntryActivity.this, msg, Toast.LENGTH_SHORT).show();
//                                if (!"findpay".equals(type)&&!"findshop".equals(type)){
//                                    finish();
//                                }else {
//                                    Intent in = new Intent();
//                                    in.setClass(WXEntryActivity.this, MyOrderActivity.class);
//                                    startActivity(in);
//                                    finish();
//                                }
//                            }
//
//                        }
//                    });

        }
    }
}
